package com.hqd.test.xml.custom;

import com.hqd.ch03.bean.User;
import com.hqd.ch03.v7.config.BeanDefinition;
import com.hqd.ch03.v7.config.MutablePropertyValues;
import com.hqd.ch03.v7.reader.xml.parse.BeanDefinitionParser;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Element;

public class UserBeanDefinitionParser implements BeanDefinitionParser {

    @Override
    public BeanDefinition parse(Element element) {
        BeanDefinition bd = new BeanDefinition();
        String id = element.getAttribute("id");
        String userName = element.getAttribute("name");
        String age = element.getAttribute("age");
        bd.setBeanClass(User.class.getName());
        bd.setId(id);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        if (StringUtils.isNotBlank(userName)) {
            propertyValues.addProperty("name", userName);
        }
        if (StringUtils.isNotBlank(age)) {
            propertyValues.addProperty("age", age);
        }
        bd.setProperties(propertyValues);
        return bd;
    }
}
